package cn.cai.service.impl;

import cn.cai.exceptionHandler.GuliException;
import cn.cai.mapper.EduCourseDescriptionMapper;
import cn.cai.pojo.EduCourse;
import cn.cai.mapper.EduCourseMapper;
import cn.cai.pojo.EduCourseDescription;
import cn.cai.pojo.vo.CourseInfoVo;
import cn.cai.pojo.vo.CourseListVo;
import cn.cai.pojo.vo.CoursePublicVo;
import cn.cai.service.IEduCourseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author caishenao
 * @since 2021-08-17
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements IEduCourseService {

    @Autowired
    private EduCourseDescriptionMapper eduCourseDescriptionMapper;
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.向课程表添加课程基本信息
        //CourseInfoVo转换为EduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        //像数据库中添加课程
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0){
            //添加失败
            throw new GuliException(20001,"添加课程信息失败");
        }

        //像课程简介表中添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(eduCourse.getId());
        insert = eduCourseDescriptionMapper.insert(eduCourseDescription);
        if (insert == 0){
            //添加课程简介失败，进行回滚,这里不实现
            throw new GuliException(20001,"添加课程简介失败");
        }
        return eduCourse.getId();

    }

    //根据courseId查询课程基本信息
    @Override
    public CourseInfoVo findCourseInfoById(String courseId) {

        //创建coursInfoVo
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //先查课程信息表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //判断是否能够查到
        if (eduCourse == null){
            throw new GuliException(20001,"没有改课程信息");
        }
        //将查询到的eduCourse类中的属性值赋值给要返回的数据类型
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //后查课程简介表
        EduCourseDescription eduCourseDescription = eduCourseDescriptionMapper.selectById(courseId);
        //这里不用判断，因为简介有时候可以为空
        //将查询到的课程简介对象赋值到要返回的数据类型
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    /**
     * 根据传入的vo对象，对数据库中的edu_course和edu_course_description两张表 进行 修改
     * @param courseInfoVo 要修改的对象，和要修改的信息
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {

        //将courseInfoVo转化为EduCourse对象
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);

        int i = 0;
        i = baseMapper.updateById(course);
        //判断受影响数是否为0 ,为0 说明没有修改成功
        if (i == 0){
            throw new GuliException(20001,"修改失败");
        }
        //修改edu_course_description中的对象
        EduCourseDescription courseDescription = new EduCourseDescription();

        //courseDescription
        BeanUtils.copyProperties(courseInfoVo,courseDescription);

        eduCourseDescriptionMapper.updateById(courseDescription);

    }

    /**
     * 调用mapper 实现根据课程id查询课程信息
     * @param courseId 课程id
     * @return 查询的课程信息
     */
    @Override
    public CoursePublicVo getPublishCourseInfo(String courseId) {
        CoursePublicVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        if (publishCourseInfo == null){
            throw new GuliException(20001,"数据查询失败");
        }
        return publishCourseInfo;
    }

    /**
     * 课程的的多条件 查询 和 分类
     * @param page 分页对象
     * @param queryCourse 要查询的对象
     * @return page对象
     */
    public IPage<EduCourse> getPageAndCondition(Page<EduCourse> page,CourseListVo queryCourse){
        return baseMapper.selectPageAndCondition(page,queryCourse);
    }


}
